package edu.spsu.library;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import edu.spsu.R;
import edu.spsu.utils.Util;


public class ShowDescription extends Activity
{
	private String TAG = ShowDescription.class.getName(), ADDRESS = "http://libraryh3lp.com/chat/spsu-ref@chat.libraryh3lp.com?skin=13157&theme=simpletext&identity=librarian&sounds=1&popout=1&sounds=1";

	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);
		setContentView(R.layout.showdescription);

		String theStory = null;

		Intent startingIntent = getIntent();

		if (startingIntent != null)
		{
			String hyperlink = startingIntent.getStringExtra("link");
			RDFItem ri = processRDF(hyperlink);

			String title = ri.getTitle() == null ? "" : ri.getTitle();
			String subtitle = ri.get_subTitle() == null ? "" : ri.get_subTitle();
			String published = ri.get_dateIssued() == null ? "" : ri.get_dateIssued();
			String publisher = ri.get_publisher() == null ? "" : ri.get_publisher();
			String desc = ri.get_classification() == null ? "" : ri.get_classification();
			
			theStory = "\n" +title + "\n\t" +subtitle + "\n\n" + published +", "+publisher
				+ "\n\n" + desc.replace('\n',' ') + "\n\nMore information:\t" + hyperlink +"\n\n";
		}
		else
		{
			theStory = "Information Not Found.";
		}

		TextView db= (TextView) findViewById(R.id.storybox);
		db.setText(theStory);
	}
	
	public void onClick(View v)
	{
		switch(v.getId())
		{
			case R.id.chat: 
				LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
				WebView wv = new WebView(ShowDescription.this);
				
				wv.loadUrl(ADDRESS);
				wv.getSettings().setJavaScriptEnabled(true);
				wv.setLayoutParams(lp);
				
				new AlertDialog.Builder(ShowDescription.this)
				.setView(wv)
				.show();
				break;
				
			case R.id.back: 
				finish();
				break;
		}
		
	}
	
	private RDFItem processRDF(String urlToRdfFeed)
	{
		try
		{
			// setup the url
			URL url = new URL(urlToRdfFeed+"/RDF");

			// create the factory
			SAXParserFactory factory = SAXParserFactory.newInstance();
			// create a parser
			SAXParser parser = factory.newSAXParser();

			// create the reader (scanner)
			XMLReader xmlreader = parser.getXMLReader();
			// instantiate our handler
			RDFHandler theRdfHandler = new RDFHandler(this);
			// assign our handler
			xmlreader.setContentHandler(theRdfHandler);
			// get our data via the url class
			InputSource is = new InputSource(url.openStream());
			// perform the synchronous parse           
			xmlreader.parse(is);
			// get the results - should be a fully populated RSSFeed instance, or null on error
			return theRdfHandler.getItem();

		}
		catch (Exception ee)
		{
			Util.report(this, TAG, ee, true);
			// if we have a problem, simply return null
			return null;
		}
	}
}
